package com.sxsy.datasource.util;

import com.sxsy.datasource.biz.HqjcData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class HqjcFileUtil {
    @Autowired
    private HqjcData hqjcData;
    ArrayList<String> fileNameList = new ArrayList();
    int mkbmindex = 0;
    int kjmcindex = 1;
    int bhindex = 2;
    int cysjindex = 3;
    int cyddindex = 4;
    int o2index = 5;
    int n2index = 6;
    int ch4index = 7;
    int co2index = 8;
    int coindex = 9;
    int c2h4index = 10;
    int c2h6index = 11;
    int c2h2index = 12;
    int c3h8index = 13;
    int wxbindex = 14;
    int statusindex = 15;

    public static Map<String, String[]> Oldmap = new HashMap<String, String[]>() ;


    public void RtfileAnalysis() throws IOException {
        //D:\Users\DELL\Desktop\data
        File file = null;
        BufferedReader br = null;//构造一个BufferedReader类来读取文件
        try {
            getAllFileName("D:\\shareData\\HQJC\\",fileNameList);
            if (fileNameList.isEmpty()){
                return;
            }
            file = new File("D:\\shareData\\HQJC\\"+fileNameList.get(0));
            br = new BufferedReader(new FileReader("D:\\shareData\\HQJC\\"+fileNameList.get(0)));
            String s = null;
            Map<String, String[]> Newmap = new HashMap<String, String[]>() ;

            int i = 0;
            while((s = br.readLine())!=null) {//使用readLine方法，一次读一行
                if (i >= 0) {
                    String[] str = s.split("\\,");

                    Newmap.put(str[bhindex], str);
                }
                i++;
            }
            if (hqjcData.DoesKeyExit().isEmpty()) {
                for (Map.Entry<String, String[]> Newentry : Newmap.entrySet()) {
                    float o2 = 0;
                    if (Newmap.get(Newentry.getKey())[o2index].isEmpty() == false) {
                        o2 = Float.parseFloat(Newmap.get(Newentry.getKey())[o2index]);
                    }

                    float n2 = 0;
                    if (Newmap.get(Newentry.getKey())[n2index].isEmpty() == false) {
                        n2 = Float.parseFloat(Newmap.get(Newentry.getKey())[n2index]);
                    }
                    float ch4 = 0;
                    if (Newmap.get(Newentry.getKey())[ch4index].isEmpty() == false) {
                        ch4 = Float.parseFloat(Newmap.get(Newentry.getKey())[ch4index]);
                    }
                    float co2 = 0;
                    if (Newmap.get(Newentry.getKey())[co2index].isEmpty() == false) {
                        co2 = Float.parseFloat(Newmap.get(Newentry.getKey())[co2index]);
                    }
                    float co = 0;
                    if (Newmap.get(Newentry.getKey())[coindex].isEmpty() == false) {
                        co = Float.parseFloat(Newmap.get(Newentry.getKey())[coindex]);
                    }
                    float c2h4 = 0;
                    if (Newmap.get(Newentry.getKey())[c2h4index].isEmpty() == false) {
                        c2h4 = Float.parseFloat(Newmap.get(Newentry.getKey())[c2h4index]);
                    }
                    float c2h6 = 0;
                    if (Newmap.get(Newentry.getKey())[c2h6index].isEmpty() == false) {
                        c2h6 = Float.parseFloat(Newmap.get(Newentry.getKey())[c2h6index]);
                    }
                    float c2h2 = 0;
                    if (Newmap.get(Newentry.getKey())[c2h2index].isEmpty() == false) {
                        c2h2 = Float.parseFloat(Newmap.get(Newentry.getKey())[c2h2index]);
                    }
                    float c3h8 = 0;
                    if (Newmap.get(Newentry.getKey())[c3h8index].isEmpty() == false) {
                        c3h8 = Float.parseFloat(Newmap.get(Newentry.getKey())[c3h8index]);
                    }
                    float wxb = 0;
                    if (Newmap.get(Newentry.getKey())[wxbindex].isEmpty() == false) {
                        wxb = Float.parseFloat(Newmap.get(Newentry.getKey())[wxbindex]);
                    }

                    hqjcData.insertRtData(Newmap.get(Newentry.getKey())[mkbmindex], Newmap.get(Newentry.getKey())[kjmcindex], Newmap.get(Newentry.getKey())[bhindex], Newmap.get(Newentry.getKey())[cysjindex], Newmap.get(Newentry.getKey())[cyddindex], o2, n2, ch4, co2, co, c2h4, c2h6, c2h2, c3h8, wxb, Newmap.get(Newentry.getKey())[statusindex]);
                }
            }else{
                if (Oldmap.size() == 0) {
                    for (int a = 0; a < hqjcData.DoesKeyExit().size(); a++) {
                        String[] strlist = new String[hqjcData.DoesKeyExit().size()];
                        Oldmap.put(hqjcData.DoesKeyExit().get(a).getCjgjk_Hqjc_Rt_Bh(), strlist);
                    }
                }
                    for(Map.Entry<String, String[]> Newentry : Newmap.entrySet()) {
                        float o2 = 0;
                        if (Newmap.get(Newentry.getKey())[o2index].isEmpty() == false) {
                            o2 = Float.parseFloat(Newmap.get(Newentry.getKey())[o2index]);
                        }

                        float n2 = 0;
                        if (Newmap.get(Newentry.getKey())[n2index].isEmpty() == false) {
                            n2 = Float.parseFloat(Newmap.get(Newentry.getKey())[n2index]);
                        }
                        float ch4 = 0;
                        if (Newmap.get(Newentry.getKey())[ch4index].isEmpty() == false) {
                            ch4 = Float.parseFloat(Newmap.get(Newentry.getKey())[ch4index]);
                        }
                        float co2 = 0;
                        if (Newmap.get(Newentry.getKey())[co2index].isEmpty() == false) {
                            co2 = Float.parseFloat(Newmap.get(Newentry.getKey())[co2index]);
                        }
                        float co = 0;
                        if (Newmap.get(Newentry.getKey())[coindex].isEmpty() == false) {
                            co = Float.parseFloat(Newmap.get(Newentry.getKey())[coindex]);
                        }
                        float c2h4 = 0;
                        if (Newmap.get(Newentry.getKey())[c2h4index].isEmpty() == false) {
                            c2h4 = Float.parseFloat(Newmap.get(Newentry.getKey())[c2h4index]);
                        }
                        float c2h6 = 0;
                        if (Newmap.get(Newentry.getKey())[c2h6index].isEmpty() == false) {
                            c2h6 = Float.parseFloat(Newmap.get(Newentry.getKey())[c2h6index]);
                        }
                        float c2h2 = 0;
                        if (Newmap.get(Newentry.getKey())[c2h2index].isEmpty() == false) {
                            c2h2 = Float.parseFloat(Newmap.get(Newentry.getKey())[c2h2index]);
                        }
                        float c3h8 = 0;
                        if (Newmap.get(Newentry.getKey())[c3h8index].isEmpty() == false) {
                            c3h8 = Float.parseFloat(Newmap.get(Newentry.getKey())[c3h8index]);
                        }
                        float wxb = 0;
                        if (Newmap.get(Newentry.getKey())[wxbindex].isEmpty() == false) {
                            wxb = Float.parseFloat(Newmap.get(Newentry.getKey())[wxbindex]);
                        }
                        hqjcData.insertHisData(Newmap.get(Newentry.getKey())[mkbmindex],Newmap.get(Newentry.getKey())[kjmcindex],Newmap.get(Newentry.getKey())[bhindex],Newmap.get(Newentry.getKey())[cysjindex].trim(),Newmap.get(Newentry.getKey())[cyddindex].trim(),o2,n2,ch4,co2,co,c2h4,c2h6,c2h2,c3h8,wxb,Newmap.get(Newentry.getKey())[statusindex]);
                        if(Oldmap.containsKey(Newentry.getKey())!=true) {
                            hqjcData.insertRtData(Newmap.get(Newentry.getKey())[mkbmindex],Newmap.get(Newentry.getKey())[kjmcindex],Newmap.get(Newentry.getKey())[bhindex],Newmap.get(Newentry.getKey())[cysjindex].trim(),Newmap.get(Newentry.getKey())[cyddindex].trim(),o2,n2,ch4,co2,co,c2h4,c2h6,c2h2,c3h8,wxb,Newmap.get(Newentry.getKey())[statusindex]);
                        }else{
                            hqjcData.updateRtData(Newmap.get(Newentry.getKey())[mkbmindex],Newmap.get(Newentry.getKey())[kjmcindex],Newmap.get(Newentry.getKey())[bhindex],Newmap.get(Newentry.getKey())[cysjindex].trim(),Newmap.get(Newentry.getKey())[cyddindex].trim(),o2,n2,ch4,co2,co,c2h4,c2h6,c2h2,c3h8,wxb,Newmap.get(Newentry.getKey())[statusindex]);
                        }

                    }
                    Oldmap = Newmap;
                }
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            br.close();
            file.delete();
            fileNameList.clear();
        }


    }
    public static void getAllFileName(String path,ArrayList<String> fileNameList) {
        //ArrayList<String> files = new ArrayList<String>();
       // boolean flag = false;
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
//	              System.out.println("文     件：" + tempList[i]);
                //fileNameList.add(tempList[i].toString());
                //tempList[i]file.getName().contains("SGDATA");

                if(tempList[i].getName().contains("SGDATA")){
                    fileNameList.add(tempList[i].getName());
                }
            }
            if (tempList[i].isDirectory()) {
//	              System.out.println("文件夹：" + tempList[i]);
                getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
            }
        }
        return;
    }

}
